package edu.mju.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.mju.eduservice.entity.StudyCourse;
import edu.mju.eduservice.entity.StudyCourseDescription;
import edu.mju.eduservice.entity.StudyTeacher;
import edu.mju.eduservice.mapper.StudyCourseMapper;
import edu.mju.eduservice.service.StudyChapterService;
import edu.mju.eduservice.service.StudyCourseDescriptionService;
import edu.mju.eduservice.service.StudyCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.mju.eduservice.service.StudyVideoService;
import edu.mju.eduservice.vo.CourseInfoVo;
import edu.mju.eduservice.vo.chapter.CourseIssueVo;
import edu.mju.eduservice.vo.frontvo.CourseFrontVo;
import edu.mju.eduservice.vo.frontvo.CourseWebVo;
import edu.mju.servicebase.exception.StuException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xiaoxin
 * @since 2020-08-31
 */
@Service
public class StudyCourseServiceImpl extends ServiceImpl<StudyCourseMapper, StudyCourse> implements StudyCourseService {

    @Autowired
    private StudyCourseDescriptionService studyCourseDescriptionService;

    @Autowired
    private StudyChapterService studyChapterService;

    @Autowired
    private StudyVideoService studyVideoService;

    /**
     * 添加课程基本信息
     * @param courseInfoVo
     */
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //课程表添加信息
        //对象转换
        StudyCourse studyCourse = new StudyCourse();
        BeanUtils.copyProperties(courseInfoVo,studyCourse);
        //TODO 课程封面无上传
        //返回添加的数量
        int insert = baseMapper.insert(studyCourse);

        if(insert == 0){
            throw new StuException(20002,"添加课程信息失败");
        }

        //获取添加后的课程id
        String cId = studyCourse.getId();

        //课程简介表添加课程简介
        StudyCourseDescription studyCourseDescription = new StudyCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,studyCourseDescription);
        //设置课程简介的id
        studyCourseDescription.setId(cId);
        studyCourseDescriptionService.save(studyCourseDescription);

        return cId;
    }

    /**
     * 根据课程id查询课程基本信息
     * @param courseId
     * @return
     */
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //查询课程表
        StudyCourse studyCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(studyCourse,courseInfoVo);

        //查询简介表
        StudyCourseDescription courseDescription = studyCourseDescriptionService.getById(courseId);
        BeanUtils.copyProperties(courseDescription,courseInfoVo);
        return courseInfoVo;
    }

    /**
     * 根据id修改
     * @param courseInfoVo
     */
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        StudyCourse studyCourse = new StudyCourse();
        BeanUtils.copyProperties(courseInfoVo,studyCourse);
        int i = baseMapper.updateById(studyCourse);

        if(i==0){
            throw new StuException(20003,"修改课程信息失败！");
        }

        //修改课程简介表
        StudyCourseDescription studyCourseDescription = new StudyCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,studyCourseDescription);
        studyCourseDescriptionService.updateById(studyCourseDescription);
    }

    /**
     * 根据课程id查找需要的数据
     * 用于课程信息确认
     * @param courseId 课程id
     * @return
     */
    @Override
    public CourseIssueVo getCourseIssueInfo(String courseId) {
        //调用mapper
        CourseIssueVo issueCourse = baseMapper.getIssueCourse(courseId);
        return issueCourse;
    }

    /**
     * 根据课程id删除小节
     * @param courseId 课程id
     */
    @Override
    public void deleteCourse(String courseId) {
        //删除小节
        studyVideoService.deleteVideo(courseId);

        //删除章节
        studyChapterService.deleteChapterByCourseId(courseId);

        //删除课程描述
        studyCourseDescriptionService.removeById(courseId);

        //删除课程
        int i = baseMapper.deleteById(courseId);
        if(i == 0){
            throw new StuException(20002,"删除课程失败");
        }
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<StudyCourse> page, CourseFrontVo courseFrontVo) {
        QueryWrapper<StudyCourse> wrapper = new QueryWrapper<>();
        //判断条件是否为空
        //一级分类
        if(!StringUtils.isEmpty(courseFrontVo.getCourseTypeParentId())){
            wrapper.eq("course_type_parent_id",courseFrontVo.getCourseTypeParentId());
        }
        //二级分类
        if(!StringUtils.isEmpty(courseFrontVo.getCourseTypeId())){
            wrapper.eq("course_type_id",courseFrontVo.getCourseTypeId());
        }
        //关注度
        if(!StringUtils.isEmpty(courseFrontVo.getSellCountSort())){
            wrapper.orderByDesc("sell_count",courseFrontVo.getSellCountSort());
        }
        //时间排序
        if(!StringUtils.isEmpty(courseFrontVo.getCreateTimeSort())){
            wrapper.orderByDesc("create_time",courseFrontVo.getCreateTimeSort());
        }
        //价格排序
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            wrapper.orderByDesc("price",courseFrontVo.getPriceSort());
        }
        //状态为已发布
        wrapper.eq("status","Normal");
        baseMapper.selectPage(page,wrapper);

        List<StudyCourse> records = page.getRecords();
        long current = page.getCurrent();
        long pages = page.getPages();
        long size = page.getSize();
        long total = page.getTotal();
        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();

        //分页数据放到map中
        Map<String,Object> map = new HashMap<>();
        map.put("records",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;
    }


    /**
     * 根据课程id查询课程信息
     * @param courseId 课程id
     * @return CourseWebVo
     */
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
